

dat <- fread("raw_data/Mayor_2_July+10,+2024_14.09/Mayor_2_July 10, 2024_14.09.csv")

colnames(dat) <- gsub("__", "_", sub("_$", "", clean_names(dat)))
dat <- dat |> 
  filter(row_number() > 2,
         substring(startdate, 1, 10) >= "2024-07-09",
         `cn_2` %in% c("Strongly disagree", "Somewhat disagree"),
         `att2` == "Red,Green",
         education != "") |> 
  mutate(group = case_when(grepl("black", fl_41_do) ~ "BLACK",
                           grepl("white", fl_41_do) ~ "WHITE",
                           T ~ "CONTROL"),
         duration_in_seconds = as.numeric(duration_in_seconds),
         across(c(dis_1, dis_2), ~ case_when(. == "None at all" ~ 0,
                                             . == "A little" ~ 1,
                                             . == "A moderate amount" ~ 2,
                                             . == "A lot" ~ 3,
                                             . == "A great deal" ~ 4)),
         white_dis = dis_1 > dis_2,
         white_dis2 = dis_1 - dis_2,
         across(starts_with("spae"), ~ case_when(. == "It almost never occurs" ~ 0,
                                                 . == "It occurs infrequently" ~ 1,
                                                 . == "It occurs occasionally" ~ 2,
                                                 . == "It is very common" ~ 3)),
         fact_spae = (spae_1 + spae_2 + spae_3 + spae_4 + spae_5 +
                        spae_6 + spae_7 + spae_8 + spae_9 + spae_10) / 30,
         fact_spae_slim = spae_1 + spae_2 + spae_3 + spae_4 + spae_5 + spae_9 + spae_10,
         across(starts_with("dw"), ~ case_when(. == "Strongly disagree" ~ 0,
                                               . == "Somewhat disagree" ~ 1,
                                               . == "Neither agree nor disagree" ~ 2,
                                               . == "Somewhat agree" ~ 3,
                                               . == "Strongly agree" ~ 4)),
         fact_dw = (dw_1 + dw_2 + dw_3 + dw_4) / 16,
         fact_rr = ((4 - dw_5) + dw_6) / 8,
         across(c(wid1, wid2, wid3), ~ case_when(grepl("at all", .) ~ 0,
                                                 grepl("Somewhat"  , .) ~ 1,
                                                 grepl("Moderately", .) ~ 2,
                                                 grepl("Very"      , .) ~ 3,
                                                 grepl("Extremely" , .) ~ 4)),
         fact_white = (wid1 + wid2 + wid3) / 12)

spae <- select(dat, responseid, starts_with("spae"))

spae <- spae[complete.cases(spae), ]

cronbach.alpha(select(spae, -responseid))

ev <- eigen(cor(spae %>% 
                  select(-responseid)))
ev$values

factors_spae <- factanal(spae %>% 
                           select(-responseid), 
                         factors = 1, 
                         scores = "regression")

spae <- cbind(spae, factors_spae$scores) %>% 
  rename(spae_factor = Factor1) %>% 
  select(responseid, spae_factor)

dat <- left_join(dat, spae)

##################
##################
##################

spae_slim <- select(dat, responseid, spae_1, spae_2, spae_3, spae_4, spae_5, spae_9, spae_10)

spae_slim <- spae_slim[complete.cases(spae_slim), ]

cronbach.alpha(select(spae_slim, -responseid))

ev <- eigen(cor(spae_slim %>% 
                  select(-responseid)))
ev$values

factors_spae <- factanal(spae_slim %>% 
                           select(-responseid), 
                         factors = 1, 
                         scores = "regression")

spae_slim <- cbind(spae_slim, factors_spae$scores) %>% 
  rename(spae_slim_factor = Factor1) %>% 
  select(responseid, spae_slim_factor)

dat <- left_join(dat, spae_slim)

##################
##################
##################
dw <- select(dat, responseid, dw_1, dw_2, dw_3, dw_4)

dw <- dw[complete.cases(dw), ]

cronbach.alpha(select(dw, -responseid))

ev <- eigen(cor(dw %>% 
                  select(-responseid)))
ev$values

factors_dw <- factanal(dw %>% 
                         select(-responseid), 
                       factors = 1, 
                       scores = "regression")

dw <- cbind(dw, factors_dw$scores) %>% 
  rename(dw_factor = Factor1) %>% 
  select(responseid, dw_factor)

dat <- left_join(dat, dw)

##################
##################
##################
white <- select(dat, responseid, starts_with("wid"))

white <- white[complete.cases(white), ]

cronbach.alpha(select(white, -responseid))

ev <- eigen(cor(white %>% 
                  select(-responseid)))
ev$values

factors_white <- factanal(white %>% 
                            select(-responseid), 
                          factors = 1, 
                          scores = "regression")

white <- cbind(white, factors_white$scores) %>% 
  rename(white_factor = Factor1) %>% 
  select(responseid, white_factor)

dat <- left_join(dat, white)

##################
##################
##################

dat <- dat |> 
  mutate(pid7 = case_when(dem_str == "Strong" ~ 1,
                          dem_str == "Not very strong" ~ 2,
                          ind_lean == "Closer to Democrats" ~ 3,
                          ind_lean == "Neither" ~ 4,
                          ind_lean == "Closer to Republicans" ~ 5,
                          gop_str == "Not very strong" ~ 6,
                          gop_str == "Strong" ~ 7),
         ideo7 = case_when(ideo == "Extremely liberal" ~ 1,
                           ideo == "Liberal" ~ 2,
                           ideo == "Slightly liberal" ~ 3,
                           ideo == "Moderate; middle of the road" ~ 4,
                           ideo == "Slightly conservative" ~ 5,
                           ideo == "Conservative" ~ 6,
                           ideo == "Extremely conservative" ~ 7),
         age = as.numeric(age),
         income = case_when(income == "Below $20,000" ~ 20000,
                            income == "$20,001-$40,000" ~ 30000,
                            income == "$40,001-$60,000" ~ 50000,
                            income == "$60,001-$80,000" ~ 70000,
                            income == "$80,001-$100,000" ~ 90000,
                            income == "$100,001-$120,000" ~ 100000,
                            income == "$120,001 or more" ~ 120000),
         fmc = case_when(grepl("Black", fmc) ~ "BLACK",
                         grepl("White", fmc) ~ "WHITE",
                         T ~ "CONTROL"),
         group_clean = paste(str_to_title(group), "City"),
         group_clean = ifelse(grepl("Control", group_clean), "Control", group_clean),
         female = factor(gender == "Female"),
         income2 = factor(income),
         t = ifelse(group_clean == "Control", "Control",
                    "Treated (Combined)"))

dat$group_clean <- relevel(factor(dat$group_clean), ref = "Control")
dat$education <- relevel(factor(dat$education), ref = "4-year college degree")

m1 <- lm(fact_spae ~ group_clean,  data =        mutate(dat, group_clean = relevel(group_clean, ref = "White City")))
m1a <- lm(fact_spae ~ group_clean, data = filter(mutate(dat, group_clean = relevel(group_clean, ref = "White City")), pid7 <= 2))
m1b <- lm(fact_spae ~ group_clean, data = filter(mutate(dat, group_clean = relevel(group_clean, ref = "White City")), pid7 > 2, pid7 < 6))
m1c <- lm(fact_spae ~ group_clean, data = filter(mutate(dat, group_clean = relevel(group_clean, ref = "White City")), pid7 >= 6))

modelsummary(list(" " = m1,
                  "(PID <= 2)" = m1a,
                  "(3 <= PID <= 5)" = m1b,
                  "(PID >= 6)" = m1c),
             statistic = "std.error",
             stars = c("†" = 0.1, "*" = 0.05, "**" = 0.01),
             coef_map = c("group_cleanBlack City" = "Black City Treatment",
                          "group_cleanControl" = "Control"),
             gof_omit = 'DF|Deviance|AIC|BIC|Within|Pseudo|Log|Std|FE|RMSE',
             title = "\\label{tab:exp-regs} Fraud Beliefs by Treatment",
             output = "latex",
             escape = FALSE,
             notes = c("Effects measured relative to individuals in the White City treatment."),
             align = "lcccc") |> 
  # kable_styling(latex_options = c("scale_down")) |> 
  # column_spec(c(1), width = "8cm") |>
  # column_spec(c(2:4), width = "4cm") |>
  kableExtra::add_header_above(header = c("", "Overall", "Democrats", "Moderates", "Republicans"),
                               line = F) |> 
  save_kable("Output/Tables/overall_exp_regs.tex")

ll <- dat |> 
  group_by(group_clean) |>
  filter(!is.na(fact_spae)) |> 
  summarise(across(fact_spae,
                   list(mean = mean, sd = sd, se = ~ sd(.) / sqrt(n()))))

ggplot(ll) +
  geom_bar(aes(x = group_clean, y = fact_spae_mean), stat = "identity",
           fill = "white", color = "black") +
  geom_errorbar(aes(x = group_clean, ymin = fact_spae_mean - fact_spae_se,
                    ymax = fact_spae_mean + fact_spae_se), width = .2) +
  theme_bc() +
  labs(x = "Treatment Arm", y = "Election Fraud Scale")

ggsave("Output/Figures/new_run_bars.png",
       width = 3, height = 2, units = "in")

ll <- dat |> 
  mutate(party = case_when(pid7 > 5 ~ "Republicans\n(PID >= 6)",
                           pid7 > 2 ~ "Moderates\n(3 <= PID <= 5)",
                           T ~ "Democrats\n(PID <= 2)")) |> 
  group_by(group_clean, party) |>
  filter(!is.na(fact_spae)) |> 
  summarise(across(fact_spae,
                   list(mean = mean, sd = sd, se = ~ sd(.) / sqrt(n()))))

ggplot(ll) +
  geom_bar(aes(x = group_clean, y = fact_spae_mean), stat = "identity",
           fill = "white", color = "black") +
  facet_grid(. ~ party) +
  geom_errorbar(aes(x = group_clean, ymin = fact_spae_mean - fact_spae_se,
                    ymax = fact_spae_mean + fact_spae_se), width = .2) +
  theme_bc() +
  labs(x = "Treatment Arm", y = "Election Fraud Scale")

ggsave("Output/Figures/new_run_bars_pid.png",
       width = 6, height = 3, units = "in")

clean_dat <- filter(dat, !is.na(fact_spae))

m2 <- lm(fact_spae ~ group_clean*fact_rr + age + female +
           education + income + pid7 + ideo7, clean_dat)

summary(m2)

m3 <- lm(fact_spae ~ group_clean*fact_dw + age + female +
           education + income + pid7 + ideo7, clean_dat)

summary(m3)

m4 <- lm(fact_spae ~ group_clean*fact_white + age + female +
           education + income + pid7 + ideo7, clean_dat)

summary(m4)

out <- bind_rows(
  ggpredict(m2, terms = c("fact_rr[all]", "group_clean")) |> 
    mutate(m = "Racial Resentment"),
  ggpredict(m3, terms = c("fact_dw[all]", "group_clean")) |> 
    mutate(m = "White Resentment Towards\nAfrican Americans"),
  ggpredict(m4, terms = c("fact_white[all]", "group_clean")) |> 
    mutate(m = "White Consciousness")
)

out$m <- factor(out$m, levels = c("Racial Resentment",
                                  "White Resentment Towards\nAfrican Americans",
                                  "White Consciousness"))

ggplot(out, aes(x = x, y = predicted,
                ymin = conf.low, ymax = conf.high,
                color = group, fill = group)) +
  facet_grid(. ~ m) +
  geom_ribbon(alpha = 0.2, color = "transparent") +
  geom_line() +
  theme_bc(legend.position = "bottom") +
  labs(y = "Election Fraud Scale", x = "Value on Scale",
       color = "Treatment Arm", fill = "Treatment Arm") +
  scale_x_continuous(labels = c("0", "0.25", "0.5", "0.75", "1"))

ggsave("Output/Figures/new_run_moderator.png",
       width = 6, height = 3, units = "in")

dat_long <- bind_rows(
  select(clean_dat, fact_spae, measure = fact_rr, group_clean) |> 
    mutate(group = "Racial Resentment"),
  select(clean_dat, fact_spae, measure = fact_dw, group_clean) |> 
    mutate(group = "White Resentment Towards\nAfrican Americans"),
  select(clean_dat, fact_spae, measure = fact_white, group_clean) |> 
    mutate(group = "White Consciousness")
)

dat_long <- bind_rows(dat_long,
                      dat_long |> 
                        filter(group_clean != "Control") |> 
                        mutate(group_clean = "Treated (Combined)"))

dat_long$group <- factor(dat_long$group, levels = c("Racial Resentment",
                                  "White Resentment Towards\nAfrican Americans",
                                  "White Consciousness"))

dat_long$group_clean <- factor(dat_long$group_clean, levels = c("Control",
                                                    "Black City",
                                                    "White City",
                                                    "Treated (Combined)"))

ggplot(filter(dat_long, group_clean != "Treated (Combined)"),
       aes(x = measure, y = fact_spae, color = group_clean, fill = group_clean)) +
  facet_grid(. ~ group) +
  geom_smooth() +
  theme_bc(legend.position = "bottom") +
  labs(y = "Election Fraud Scale", x = "Value on Scale",
       color = "Treatment Arm", fill = "Treatment Arm") +
  scale_x_continuous(labels = c("0", "0.25", "0.5", "0.75", "1"))

ggsave("Output/Figures/non_para_all.png",
       width = 6, height = 3, units = "in")

ggplot(filter(dat_long, group_clean %in% c("Treated (Combined)", "Control")),
       aes(x = measure, y = fact_spae, color = group_clean, fill = group_clean)) +
  facet_grid(. ~ group) +
  geom_smooth() +
  theme_bc(legend.position = "bottom") +
  labs(y = "Election Fraud Scale", x = "Value on Scale",
       color = "Treatment Arm", fill = "Treatment Arm") +
  scale_x_continuous(labels = c("0", "0.25", "0.5", "0.75", "1"))

ggsave("Output/Figures/non_para_combined.png",
       width = 6, height = 3, units = "in")


m2b <- lm(fact_spae ~ t*fact_rr + age + female +
            education + income + pid7 + ideo7, clean_dat)

summary(m2b)

m3b <- lm(fact_spae ~ t*fact_dw + age + female +
            education + income + pid7 + ideo7, clean_dat)

summary(m3b)

m4b <- lm(fact_spae ~ t*fact_white + age + female +
            education + income + pid7 + ideo7, clean_dat)

summary(m4b)

out <- bind_rows(
  ggpredict(m2b, terms = c("fact_rr[all]", "t")) |> 
    mutate(m = "Racial Resentment"),
  ggpredict(m3b, terms = c("fact_dw[all]", "t")) |> 
    mutate(m = "White Resentment Towards\nAfrican Americans"),
  ggpredict(m4b, terms = c("fact_white[all]", "t")) |> 
    mutate(m = "White Consciousness")
)

out$m <- factor(out$m, levels = c("Racial Resentment",
                                  "White Resentment Towards\nAfrican Americans",
                                  "White Consciousness"))

ggplot(out, aes(x = x, y = predicted,
                ymin = conf.low, ymax = conf.high,
                color = group, fill = group)) +
  facet_grid(. ~ m) +
  geom_ribbon(alpha = 0.2, color = "transparent") +
  geom_line() +
  theme_bc(legend.position = "bottom") +
  labs(y = "Election Fraud Scale", x = "Value on Scale",
       color = "Treatment Arm", fill = "Treatment Arm") +
  scale_x_continuous(labels = c("0", "0.25", "0.5", "0.75", "1"))

ggsave("Output/Figures/new_run_moderator_comb.png",
       width = 6, height = 3, units = "in")


m2c <- lm(fact_spae ~ group_clean*fact_rr + age + female +
            education + income + pid7 + ideo7, mutate(clean_dat, group_clean = relevel(group_clean, ref = "White City")))

m3c <- lm(fact_spae ~ group_clean*fact_dw + age + female +
            education + income + pid7 + ideo7, mutate(clean_dat, group_clean = relevel(group_clean, ref = "White City")))

m4c <- lm(fact_spae ~ group_clean*fact_white + age + female +
            education + income + pid7 + ideo7, mutate(clean_dat, group_clean = relevel(group_clean, ref = "White City")))

##################
##################
##################

list1 <- list("Classic Racial Resentment" = m2,
              "White Resentment Toward African Americans" = m3,
              "White Consiousness" = m4)

list2 <- list("Classic Racial Resentment" = m2b,
              "White Resentment Toward African Americans" = m3b,
              "White Consiousness" = m4b)

list3 <- list("Classic Racial Resentment" = m2c,
              "White Resentment Toward African Americans" = m3c,
              "White Consiousness" = m4c)

lol <- list(list1, list2, list3)

lapply(c(1:3), function(l){
  
  if(l == 1){
    t_name = "full_inter_reg.tex"
    reg_lab = "inter-reg-full"
  }else if(l == 2){
    t_name = "pooled_inter_reg.tex"
    reg_lab = "inter-reg-pool"
  }else{
    t_name = "full_inter_reg_white.tex"
    reg_lab = "inter-reg-full-white"
  }
  
  if(l == 3){
    note_add <- "Treatment relative to white city treatment."
  }else{
    note_add <- "Treatment relative to control."
  }
  
  modelsummary(lol[[l]],
               statistic = "std.error",
               stars = c("†" = 0.1, "*" = 0.05, "**" = 0.01),
               coef_map = c("fact_rr" = "Antipathetic Measure",
                            "fact_dw" = "Antipathetic Measure",
                            "fact_white" = "Antipathetic Measure",
                            
                            "group_cleanBlack City" = "Black City Treatment",
                            "group_cleanBlack City:fact_rr" = "Black City Treatment × Antipathetic Measure",
                            "group_cleanBlack City:fact_dw" = "Black City Treatment × Antipathetic Measure",
                            "group_cleanBlack City:fact_white" = "Black City Treatment × Antipathetic Measure",
                            
                            "group_cleanWhite City" = "White City Treatment",
                            "group_cleanWhite City:fact_rr" = "White City Treatment × Antipathetic Measure",
                            "group_cleanWhite City:fact_dw" = "White City Treatment × Antipathetic Measure",
                            "group_cleanWhite City:fact_white" = "White City Treatment × Antipathetic Measure",
                            
                            "group_cleanControl" = "Control",
                            "group_cleanControl:fact_rr" = "Control × Antipathetic Measure",
                            "group_cleanControl:fact_dw" = "Control × Antipathetic Measure",
                            "group_cleanControl:fact_white" = "Control × Antipathetic Measure",
                            
                            "tTreated (Combined)" = "Pooled Treatment",
                            "tTreated (Combined):fact_rr" = "Pooled Treatment × Antipathetic Measure",
                            "tTreated (Combined):fact_dw" = "Pooled Treatment × Antipathetic Measure",
                            "tTreated (Combined):fact_white" = "Pooled Treatment × Antipathetic Measure",
                            
                            "age" = "Age",
                            
                            "femaleTRUE" = "Female",
                            
                            "educationDid not graduate from high school" = "No High School Degree",
                            "educationHigh school graduate" = "High School Graduate",
                            "educationSome college, but no degree" = "Some College",
                            "education2-year college degree" = "2-year college degree",
                            "Postgraduate degree (MA, MBA, MD, JD, PhD, etc.)" = "Postgraduate Degree",
                            
                            "income" = "Income",
                            "pid7" = "7-Point Party ID",
                            "ideo7" = "7-Point Ideology"
               ),
               gof_omit = 'DF|Deviance|AIC|BIC|Within|Pseudo|Log|Std|FE|RMSE',
               title = paste0("\\label{tab:", reg_lab, "} Fraud Beliefs: Interactions Between Antipathetic Measures and Treatment"),
               output = "latex",
               escape = FALSE,
               notes = c(note_add,
                         "Education measured relative to respondents with 4-year college degree.")) |> 
    kable_styling(latex_options = c("scale_down")) |> 
    column_spec(c(1), width = "8cm") |>
    column_spec(c(2:4), width = "4cm") |>
    save_kable(paste0("Output/Tables/",
                      t_name))
})

##################
##################
##################
cleanup("clean_dat")

clean_dat$college <- as.integer(clean_dat$education %in% 
  c("4-year college degree", "Postgraduate degree (MA, MBA, MD, JD, PhD, etc.)"))

clean_dat$female <- as.integer(clean_dat$female == "TRUE")

con <- filter(clean_dat, group_clean == "Control")


ks_outs <- lapply(c("White City", "Black City"), function(r){
  
  tr <- filter(clean_dat, group_clean == r)
  
  rbindlist(lapply(c("fact_rr", "fact_dw", "fact_white", "age", "female",
                     "college", "income", "pid7", "ideo7"), function(v){
                       
                       tr <- mutate(tr, target := !!sym(v))
                       con <- mutate(con, target := !!sym(v))
                       if(v == "lpopulation"){
                         v <- "population"
                       }
                       return(data.table(var = v,
                                         star_raw = ks_test(tr$target, con$target)[["p.value"]] < 0.05,
                                         
                                         cross_raw = t.test(tr$target, con$target)[["p.value"]] < 0.05))
                     }))
})

ks_outs <- left_join(rename(ks_outs[[1]], star_raw_white = star_raw, cross_raw_white = cross_raw),
                     rename(ks_outs[[2]], star_raw_black = star_raw, cross_raw_black = cross_raw))

ll <- clean_dat |> 
  group_by(group_clean) |> 
  summarize(across(c("fact_rr", "fact_dw", "fact_white", "age", "female",
                     "college", "income", "pid7", "ideo7"), ~ mean(., na.rm = T))) |> 
  pivot_longer(cols = c("fact_rr", "fact_dw", "fact_white", "age", "female",
                        "college", "income", "pid7", "ideo7")) |> 
  mutate(value = case_when(name %in% c("fact_rr", "fact_dw", "fact_white",
                                       "pid7", "ideo7") ~ comma(value, 0.01),
                           name == "age" ~ comma(value),
                           name %in% c("female", "college") ~ percent(value),
                           T ~ dollar(value))) |> 
  pivot_wider(id_cols = name, names_from = group_clean, values_from = value)

ll <- left_join(ll, ks_outs, by = c("name" = "var")) |> 
  mutate(`Black City` = ifelse(cross_raw_black, paste0(Black, "*"), `Black City`),
         `Black City` = ifelse(star_raw_black, paste0(Black, "†"),  `Black City`),
         `White City` = ifelse(cross_raw_white, paste0(White, "*"), `White City`),
         `White City` = ifelse(star_raw_white, paste0(White, "†"),  `White City`)) |> 
  select(-starts_with("star"), -starts_with("cross"))

ll <- ll |> 
  mutate(name = case_match(name,
                           "fact_rr" ~ "Classic Racial Resentment",
                           "fact_dw" ~ "White Resentment Against African Americans",
                           "fact_white" ~ "White Consciousness",
                           "age" ~ "Age",
                           "female" ~ "Female",
                           "college" ~ "4 Year Degree or More",
                           "income" ~ "Income",
                           "pid7" ~ "7-Point Party ID",
                           "ideo7" ~ "7-Point Ideology"))

kable(select(ll, ` ` = name,
             Control, `Black City`, `White City`),
      "latex", caption = "\\label{tab:rando} Randomization Check",
      linesep = "", align = c("l", rep("c", 3)),
      booktabs = T, escape = T) |> 
  # add_header_above(c(" " = 1, "Means: Unweighted Data" = 3, "Means: Unweighted Data" = 3), align = "c") |> 
  # column_spec(c(1), width = "4cm") |>
  # column_spec(c(2:4), width = "3cm") |>
  kable_styling(latex_options = c("striped")) |> 
  footnote(general_title = "Notes: ",
           list("* Mean different from control respondents (t-test, p < 0.05).",
                "† Distribution different from control respondents (Kolmogorov–Smirnov test, p < 0.05).")) |> 
  save_kable("Output/Tables/rando.tex")
